import {defineStore} from "pinia";
import {model, msg, request} from "@ut/tool";

export const emigrationStore = defineStore('emigrationStore', {
    state: () => ({
        loading: false,
        list: [],
        total: 0,
    }),
    actions: {
        // 查询
        get(params) {
            return new Promise(async t => {
                this.loading = true;
                let {data, total} = await request({url: '/emigration/get', method: 'get', params});
                this.list = data;
                this.total = total;
                this.loading = false;
                t();
            })
        },

        // 创建
        create(data) {
            return new Promise(async t => {
                this.loading = true;
                await request({url: '/emigration/create', method: 'post', data});
                msg('提交成功，等待管理员审核');
                t();
            })
        },

        // 修改
        update(data) {
            return new Promise(async t => {
                this.loading = true;
                await request({url: '/emigration/update', method: 'put', data});
                msg('修改成功');
                t();
            })
        },

        // 删除
        remove(id) {
            return new Promise(async t => {
                let res = await model('提示', '正在删除迁出记录，是否继续？', 'warning');
                if (res) {
                    this.loading = true;
                    await request({url: '/emigration/remove', method: 'delete', params: {id}});
                    msg('删除成功');
                    t();
                }
            })
        },
    }
})